package com.jintian.smart.kernel.orm.compatible;

import com.jintian.smart.kernel.common.util.ObjectUtil;
import com.jintian.smart.kernel.common.util.StringUtil;
import com.jintian.smart.kernel.core.domain.PageQuery;
import org.springframework.core.MethodParameter;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

public class PageArgumentResolver implements HandlerMethodArgumentResolver {
    private static final String ORDER_ASC = "asc";
    private static final String COMMA = ",";

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return PageQuery.class.equals(parameter.getParameterType());
    }

    /**
     * page 参数解析
     *
     * @param parameter     MethodParameter
     * @param mavContainer  ModelAndViewContainer
     * @param request       NativeWebRequest
     * @param binderFactory WebDataBinderFactory
     * @return 检查后新的page对象
     */
    @Override
    public Object resolveArgument(@NonNull MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest request, WebDataBinderFactory binderFactory) {
        // 分页参数 page: 0, size: 10, sort=id%2Cdesc
        String pageParam = request.getParameter("page");
        String sizeParam = request.getParameter("size");
        String[] sortParam = request.getParameterValues("sort");
        PageQuery page = new PageQuery<>();
        if (StringUtil.isNotBlank(pageParam)) {
            page.setLimit(Integer.parseInt(pageParam));
        }
        if (StringUtil.isNotBlank(sizeParam)) {
            page.setPageSize(Integer.parseInt(sizeParam));
        }
        if (ObjectUtil.isEmpty(sortParam)) {
            return page;
        }
        for (String param : sortParam) {
            if (StringUtil.isBlank(param)) {
                continue;
            }
            String[] split = param.split(COMMA);
            // 清理字符串
//            OrderItem orderItem = new OrderItem();
//            orderItem.setColumn(cleanIdentifier(split[0]));
//            orderItem.setAsc(isOrderAsc(split));
//            page.addOrder(orderItem);
        }
        return page;
    }

    private static boolean isOrderAsc(String[] split) {
        // 默认 desc
        if (split.length < 2) {
            return false;
        }
        return ORDER_ASC.equalsIgnoreCase(split[1]);
    }

    private static String cleanIdentifier(String param) {
        if (param == null) {
            return null;
        }
        StringBuilder paramBuilder = new StringBuilder();
        for (int i = 0; i < param.length(); i++) {
            char c = param.charAt(i);
            if (Character.isJavaIdentifierPart(c)) {
                paramBuilder.append(c);
            }
        }
        return paramBuilder.toString();
    }
}
